package com.sxkiler.demo.easy;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.*;
import com.sxkiler.demo.model.*;

/**
add-strings=字符串相加
<p>给定两个字符串形式的非负整数&nbsp;<code>num1</code> 和<code>num2</code>&nbsp;，计算它们的和。</p>

<p>&nbsp;</p>

<p><strong>提示：</strong></p>

<ol>
	<li><code>num1</code> 和<code>num2</code>&nbsp;的长度都小于 5100</li>
	<li><code>num1</code> 和<code>num2</code> 都只包含数字&nbsp;<code>0-9</code></li>
	<li><code>num1</code> 和<code>num2</code> 都不包含任何前导零</li>
	<li><strong>你不能使用任何內建 BigInteger 库，&nbsp;也不能直接将输入的字符串转换为整数形式</strong></li>
</ol>
解法参考addBinary,只不过2进位，变成了10进位
 */
public class addStrings {
    

    class Solution {
        public String addStrings(String param0,String param1) {
            StringBuffer ans = new StringBuffer();

            int n = Math.max(param0.length(), param1.length()), carry = 0;
            int len_a = param0.length();
            int len_b = param1.length();
            for (int i = 1; i <= n; ++i) {
                carry += i <= len_a ? (param0.charAt(len_a - i) - '0') : 0;
                carry += i <= len_b ? (param1.charAt(len_b - i) - '0') : 0;
                //尾值
                ans.append((char) (carry % 10 + '0'));
                //进位
                carry /= 10;
            }

            if (carry > 0) {
                ans.append('1');
            }
            ans.reverse();
            return ans.toString();
        }
    }

    @Test
    public void test(){
        Solution solution = new Solution();
        Assertions.assertEquals(solution.addStrings("390","390"),"780");
        Assertions.assertEquals(solution.addStrings("190","0"),"190");
    }
}

